package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 王健
 * @version 1.0
 */
@Controller
@RequestMapping(value="/role")
public class RoleController extends BaseController {

    @DubboReference
    private RoleService roleService;

    @DubboReference
    private PermissionService permissionService;

    private final static String PAGE_INDEX = "role/index";


    /**
     * 列表
     * @param
     * @return
     */
/*    @RequestMapping
    public String index(ModelMap model) {
        List<Role> list = roleService.findAll();
        model.addAttribute("list", list);
        return PAGE_INDEX;
    }*/

    //结合分页展现角色管理列表信息
    @RequestMapping
    public String index(HttpServletRequest request, Model model) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        model.addAttribute("page", pageInfo);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    // 跳转到新增弹窗
    @GetMapping("/create")
    public String creat() {
        return "role/create";
    }

    //实现角色管理用户新增
    /*@PostMapping("/save")
    public String save(Role role) {
        roleService.insert(role);
        return "redirect:/role";
    }*/

    // 新增角色信息
    @PostMapping("/save")
    public String save(Role role) {
        roleService.insert(role);
        return "common/successPage";
    }

    /**
     * 修改角色信息
     * 先要通过Id获取要修改的用户信息
     * 在前端页面进行回显
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @PostMapping("/update")
    public String update(Role role) {
        roleService.update(role);
        return "common/successPage";
    }

    // 删除角色管理用户
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        roleService.delete(id);
        return "redirect:/role";
    }

    //进入分配权限页面
    @GetMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable("roleId") Long roleId, Model model) {
        //1.将roleId存储到请求域
        model.addAttribute("roleId", roleId);
        //2.调用业务层方法查询所有权限(并且要满足客户端需要的形状)
        List<Map<String,Object>> zNodes = permissionService.findPermissionByRoleId(roleId);
        //3.将zNodes转成JSON串存储到请求域
        model.addAttribute("zNodes", JSON.toJSON(zNodes));
        return "role/assignShow";
    }

    //给角色分配权限
    @PostMapping("/assignPermission")
    public String assignPermission(@RequestParam("roleId") Long roleId,
                                   @RequestParam("permissionIds") List<Long> permissionIds) {
        //1.调用业务层方法保存给角色分配权限
        permissionService.saveRolePermission(roleId, permissionIds);
        //2.显示成功页面
        return "common/successPage";
    }


}
